<?php

$start = $_GET['start'];
$order = $_GET['order'];

session_start();
header("Cache-control: private");

$valid_user = "1";

$_SESSION['valid_user'] = $valid_user;

?>

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=ISO-8859-1">
<title>Rideshare</title>
<link rel="stylesheet" type="text/css" href="../external.css">
<SCRIPT LANGUAGE="JavaScript">
<!--
function detail(id) { 
	URL = "detail.php?id=" + id;
	popup_detail = window.open(URL, 'popup_detail', 'width=480, height=400, toolbar=1, location=0, statusbar=0, menubar=1, resizable=1');
}
// -->
</script>
</head>

<?php 
include("../header.php") ;

include('../dbconnect.php'); 

if (!$start) { $start = 0; $maxitems = 100; }
else { $maxitems = 100; }

if ($order=='type') {
	$sortorder = "type, frequency, date, orig_city";
} else if ($order=='date') {
	$sortorder = "frequency, date, orig_city"; 
} else if ($order=='start') {
	$sortorder = "orig_prov, orig_city, frequency, date";
} else if ($order=='end') {
	$sortorder = "dest_prov, dest_city, frequency, date";
} else if ($order=='name') {
	$sortorder = "name";
} else {
	$sortorder = "frequency, date, orig_city";
} 


$query = "SELECT id, type, name, orig_city, orig_prov, dest_city, dest_prov, date, margin, frequency FROM rides WHERE date >= DATE_ADD(NOW(), INTERVAL -(margin+1) DAY) ORDER BY $sortorder LIMIT $start,$maxitems";
//echo $query;
$result = mysql_query($query, $db);
$num_rows = mysql_num_rows($result);
?>
<center><p><table border="0" cellpadding="2" cellspacing="0" width="550">
<tr><td><center><h1>Current Rides Page</h1>
<p><a href="rides_local.php">View local or commuter trips &#187;</a><br/>

<script type="text/javascript"><!--
google_ad_client = "pub-9558928185904351";
/* 728x90, created 3/26/09 rideshare */
google_ad_slot = "3448229095";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

<p><b>Note:</b> Click on the column headers to sort rides by type, city or date.<br> View details about each ride by clicking on the links below. <br></center></td></tr>
</table><p>
<?php
echo "<table border=0 cellpadding=1 cellspacing=0 width=95% align=center bgcolor=#666633><tr><td>";
echo "<table border=0 cellpadding=4 cellspacing=1 width=100% bgcolor=#FFFFFF>";
echo "<tr><th width=15%><a href='$PHP_SELF?order=type'>Ride Type</a></th><th width=15%><a href='$PHP_SELF?order=start'>Starting Point</a></th><th width=15%><a href='$PHP_SELF?order=end'>Destination</a></th><th width=15%><a href='$PHP_SELF?order=name'>Name</a></th><th width=20%><a href='$PHP_SELF?order=date'>Date</a></th></tr>";

while ($myrow = mysql_fetch_array($result)) {
	$id = $myrow["id"];
	$type = $myrow["type"];
	$name = $myrow["name"];
	$orig_city = $myrow["orig_city"];
	$orig_prov = $myrow["orig_prov"];
	$dest_city = $myrow["dest_city"];
	$dest_prov = $myrow["dest_prov"];
	$date = $myrow["date"];
	list ($year, $month, $day) = explode ("-", $date);
	$date = date("F j, Y", mktime(0,0,0, $month, $day, $year));
	$margin = $myrow["margin"];
	//$phone = $myrow["phone"];
	//$name = $myrow["name"];
	$frequency = $myrow["frequency"];
	//$costs = $myrow["costs"];
	if ($type=="looking") { $typemessage = "Need a Ride";  $rowcolor = "#fffff";}
	if ($type=="offering") { $typemessage = "Offering a Ride";  $rowcolor = "#ffddaa";}
	if ($margin) { $margin_note = "&plusmn;$margin days"; } else { $margin_note =""; }
	if ($frequency) { $date_note = "Regular Trip: <span class=smaller>($frequency)</span>"; }
	else { $date_note = "$date $margin_note"; }
	
	echo "<tr bgcolor='$rowcolor'>\n";
	echo "<td>$typemessage</td>\n";
	echo "<td>$orig_city $orig_prov --&gt;</td>\n";
	echo "<td>$dest_city $dest_prov</td>\n";
	echo "<td><a href=\"detail.php?id=$id\">$name</a></td>\n";
	echo "<td>$date_note</td>\n";
	echo "</tr>\n";
}
echo "</table>";
echo "</td></tr></table>";

$totalresult = mysql_query("SELECT id FROM rides WHERE date >= DATE_ADD(NOW(),  INTERVAL -margin -1 DAY) ", $db);
$total = mysql_num_rows($totalresult);
echo "<p>(" . ($start+1) . " - " . ($start + $num_rows) . " of " . $total . ") ";
if ($num_rows == $maxitems) { echo " &nbsp; <b><a href=\"$PHP_SELF?start=" . ($start + $maxitems) . "&order=$order\">MORE >></a></b></p>"; }

?>

<script type="text/javascript"><!--
google_ad_client = "pub-9558928185904351";
/* 728x90, created 3/26/09 rideshare */
google_ad_slot = "3448229095";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
		
<?php
include("../footer.php"); 
?>
</html>
